package com.iss.airsys.controller;


import com.iss.airsys.entity.Guest;
import com.iss.airsys.model.GuestWithRoomfree;
import com.iss.airsys.model.MyResponseData;
import com.iss.airsys.service.GuestService;
import com.iss.airsys.service.RoomfreeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("guest/")
public class GuestController {
    @Autowired
    private GuestService service;
    @Autowired
    private RoomfreeService roomfreeService;
    //办理入住.添加登记客户信息
    @RequestMapping("insertGuest")
    public MyResponseData insertGuest(Guest guest){
        MyResponseData responseData = new MyResponseData();
        //先去查询房间状态
        String state=roomfreeService.selectRoomState(guest.getGuest_roomid());
        //入住时间
        Date date = new Date();
        System.out.println("shijanshi ========="+date);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");
        String format = simpleDateFormat.format(date);
        System.out.println("shijanshi ========="+format);
        guest.setGuest_checkintime(format);
        //插入
        Integer count = service.insertGuest(guest);
        //更改状态
        Integer count1 = roomfreeService.updateRoomState("已入住",guest.getGuest_roomid());
        if (state.equals("已入住")){
            responseData.setErrorCode(-1);
            responseData.setStatus(guest.getGuest_roomid()+"房间已满");
        }else{
            if(count>0 && count1>0){
                responseData.setErrorCode(0);
                responseData.setStatus("办理入住成功");
            }else{
                responseData.setErrorCode(-1);
                responseData.setStatus("办理入住失败");
            }
        }


        return responseData;
    }
    @RequestMapping("selectAllGuest")
    public List<GuestWithRoomfree> selectAllGuest(){
        MyResponseData responseData = new MyResponseData();
        List<GuestWithRoomfree> guest = service.selectAllGuest();
        System.out.println(guest);
        responseData.setObject(guest);
        responseData.setErrorCode(0);
        responseData.setStatus("success");
        return guest;
    }

    @RequestMapping("insertGuestforGuestlist")
    public MyResponseData insertGuestforGuestlist(Guest guest){
        MyResponseData responseData = new MyResponseData();
        Integer count = service.insertGuest(guest);
        if (count > 0) {
            responseData.setErrorCode(0);
            responseData.setStatus("添加成功");
        } else {
            responseData.setErrorCode(-1);
            responseData.setStatus("添加失败");
        }
        return responseData;
    }

    //更新编辑操作
    @RequestMapping("updateGuest")
    public MyResponseData updateGuest(Guest guest){
        MyResponseData responseData = new MyResponseData();
        Integer count = service.updateGuest(guest);
        if(count>0){
            responseData.setErrorCode(0);
            responseData.setStatus("更新客户信息成功");
        }else{
            responseData.setErrorCode(-1);
            responseData.setStatus("更新失败");
        }
        return responseData;
    }


    //删除操作
    @RequestMapping("deleteGuest")
    public MyResponseData deleteGuest(Integer[] guest_id){
        MyResponseData responseData = new MyResponseData();
        try {
            Integer count = service.deleteGuest(guest_id);
            if(count>0){
                responseData.setErrorCode(0);
                responseData.setStatus("删除成功");
            }else{
                responseData.setErrorCode(-1);
                responseData.setStatus("删除失败");
            }
        }catch (Exception e){
            responseData.setErrorCode(-1);
            responseData.setStatus("请选择要清除的客户信息");
        }
        return responseData;
    }
    @RequestMapping("selectGuestById")
    public MyResponseData selectGuestById(Integer guest_id){
        MyResponseData responseData = new MyResponseData();
        Guest guest = service.selectGuestById(guest_id);
        if(guest!=null){
            responseData.setErrorCode(0);
            responseData.setStatus("success");
            responseData.setObject(guest);
        }
        return responseData;
    }


}
